/**
 * Scene utility functions
 * Requirements: 1.1, 1.5
 */

import { FloatingObject, FloatingObjectType } from '@/types';

/**
 * Predefined floating object configurations
 */
export const FLOATING_OBJECT_CONFIGS = [
  { id: 'happy-face', type: 'emoji' as FloatingObjectType },
  { id: 'sad-face', type: 'emoji' as FloatingObjectType },
  { id: 'crying-face', type: 'emoji' as FloatingObjectType },
  { id: 'egg-tart', type: 'food' as FloatingObjectType },
  { id: 'cake', type: 'food' as FloatingObjectType },
  { id: 'cloud', type: 'nature' as FloatingObjectType },
  { id: 'sun', type: 'nature' as FloatingObjectType },
  { id: 'star', type: 'nature' as FloatingObjectType },
  { id: 'moon', type: 'nature' as FloatingObjectType },
];

/**
 * Generate random position within bounds
 */
function randomPosition(min: number, max: number): number {
  return Math.random() * (max - min) + min;
}

/**
 * Generate a floating object with random position and rotation
 */
export function generateFloatingObject(
  id: string,
  type: FloatingObjectType,
  index: number
): FloatingObject {
  return {
    id,
    type,
    modelPath: '', // Not used for emoji-based rendering
    position: {
      x: randomPosition(-15, 15),
      y: randomPosition(-10, 10),
      z: randomPosition(-5, 5),
    },
    rotation: {
      x: randomPosition(0, Math.PI * 2),
      y: randomPosition(0, Math.PI * 2),
      z: randomPosition(0, Math.PI * 2),
    },
    scale: randomPosition(0.8, 1.5),
    animationSpeed: randomPosition(0.5, 2),
  };
}

/**
 * Generate initial scene objects
 * Requirements: 1.1, 1.5 - Must have at least 10 objects with one from each category
 */
export function generateInitialSceneObjects(): FloatingObject[] {
  const objects: FloatingObject[] = [];
  
  // Generate objects from predefined configs
  FLOATING_OBJECT_CONFIGS.forEach((config, index) => {
    objects.push(generateFloatingObject(config.id, config.type, index));
  });
  
  // Add more objects to reach at least 10
  const additionalCount = Math.max(0, 10 - objects.length);
  for (let i = 0; i < additionalCount; i++) {
    const config = FLOATING_OBJECT_CONFIGS[i % FLOATING_OBJECT_CONFIGS.length];
    objects.push(
      generateFloatingObject(`${config.id}-${i}`, config.type, objects.length)
    );
  }
  
  return objects;
}

/**
 * Validate scene initialization
 * Checks if scene has at least 10 objects and includes all categories
 */
export function validateSceneInitialization(objects: FloatingObject[]): boolean {
  // Check minimum count
  if (objects.length < 10) {
    return false;
  }
  
  // Check for at least one object from each category
  const hasEmoji = objects.some(obj => obj.type === 'emoji');
  const hasFood = objects.some(obj => obj.type === 'food');
  const hasNature = objects.some(obj => obj.type === 'nature');
  
  return hasEmoji && hasFood && hasNature;
}
